//
//  YTAuthorInfoController.m
//  yitaicaijing
//
//  Created by DIANGHONG on 2018/9/17.
//  Copyright © 2018年 DIANGHONG. All rights reserved.
//

#import "YTAuthorInfoController.h"
#import "YTAuthorInfoHeaderView.h"
#import "YTAuthorPostArticleModel.h"
#import "YTHomeArticleCell.h"
@interface YTAuthorInfoController ()<UITableViewDataSource ,UITableViewDelegate>

@property(nonatomic ,strong)UITableView *tableView;
@property(nonatomic ,strong)YTAuthorPostArticleModel *model;

@property(nonatomic ,strong)YTAuthorInfoHeaderView *headerView;


@end

@implementation YTAuthorInfoController

// lazy headerView
- (YTAuthorInfoHeaderView *)headerView
{
    if (!_headerView) {
        _headerView = [YTAuthorInfoHeaderView headerView];
        _headerView.followButtonClick = ^(NSString *ID,BOOL isFollow) {
            __weak typeof(self) WeakSelf = self;
            if (!YTNetworkManager.shareManager.account.auth) {
                YTLoginViewController *logVC = [[YTLoginViewController alloc] init];
                [WeakSelf.navigationController pushViewController:logVC animated:true];
                return ;
            }
            
            [YTNetworkManager.shareManager requestFollowAuthorWithFollowID:ID
                                                                  isFollow:isFollow
                                                                completion:^(BOOL isSucc, BOOL isReward, NSString *error)
            {
                if (!isSucc) {
                    [self.view showErrorWithTipStr:error];
                    return ;
                }
                
                 isReward ? [self.view showAwardWithTipStr:[NSString stringWithFormat:@"关注成功 +%@算力",error]] : [self.view showTipWithTipStr:error];
                [self loadDataWithPullUp:false];
            }];
            
            
        };
    }
    return _headerView;
}

- (UITableView *)tableView
{
    if (!_tableView) {
        _tableView = [[UITableView alloc] initWithFrame:self.view.bounds];
        _tableView.tableHeaderView = self.headerView;
        _tableView.sectionHeaderHeight = 115;
        _tableView.rowHeight = 110;
        _tableView.dataSource = self;
        _tableView.delegate = self;
        
        
        [_tableView registerNib:[UINib nibWithNibName:@"YTHomeArticleCell" bundle:nil] forCellReuseIdentifier:@"articleCellID"];
        
        _tableView.separatorStyle = UITableViewCellSelectionStyleNone;
        
        // setup refresh
        YTRefreshHeader *header = [YTRefreshHeader headerWithRefreshingBlock:^{
            [self loadDataWithPullUp:false];
        }];
        
        MJRefreshAutoNormalFooter *footer = [MJRefreshAutoNormalFooter footerWithRefreshingBlock:^{
            [self loadDataWithPullUp:YES];
        }];
        
        [footer setTitle:@"" forState:MJRefreshStateIdle];
        [footer setTitle:@"" forState:MJRefreshStatePulling];
        [footer setTitle:@"没有更多了哦~" forState:MJRefreshStateNoMoreData];

        
        _tableView.mj_header = header;
        _tableView.mj_footer = footer;
        
        
    }
    return _tableView;
}

- (void)viewDidLoad {
    [super viewDidLoad];

    
    [self.view addSubview:self.tableView];
    
    [self loadDataWithPullUp:false];
    
    // setup nav
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"倛袨1"]
                                                                             style:UIBarButtonItemStylePlain
                                                                            target:self action:@selector(backButtonClick)];
    
    self.tableView.backgroundColor = [UIColor colorWithHex:@"f1f2f2"];
    
    [self.navigationController.navigationBar setTintColor:kBackButtonColor];

}

#pragma mark: - 返回按钮 监听
- (void)backButtonClick
{
    [self.navigationController popViewControllerAnimated:true];
}

#pragma mark: - 加载数据
- (void)loadDataWithPullUp:(BOOL)pullUp
{
    if (pullUp) {
        // 等于总条数 就没有更多了
        if (self.model.list.count == self.model.total.intValue) {
            [self.tableView.mj_footer endRefreshingWithNoMoreData];
            return;
        }
    }
    
    [YTNetworkManager.shareManager requestAuthorArticleWithUserID:self.user_id
                                                        page_size:@"10"
                                                       menu_order:pullUp ? self.model.list.lastObject.menu_order : @"1"
                                                          maxtime:nil
                                                          mintime:pullUp ? self.model.list.lastObject.post_date : nil
                                                       completion:^(YTAuthorPostArticleModel *model, BOOL isSucc, NSString *error)
    {
        if (!isSucc) {
            [self.view showErrorWithTipStr:error];
            return ;
        }
        
        if (!pullUp) {
            self.model = model;
            [self.tableView.mj_header endRefreshing];
        }else{
            self.model.list = [self.model.list arrayByAddingObjectsFromArray:model.list];
            [self.tableView.mj_footer endRefreshing];

        }
        self.headerView.authorInfo = self.model.author_info;

        [self.tableView reloadData];
        
    }];
}

#pragma mark: - table view data source
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.model.list.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    YTHomeArticleCell *cell = [tableView dequeueReusableCellWithIdentifier:@"articleCellID" forIndexPath:indexPath];
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    cell.article = self.model.list[indexPath.row];
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    
 
    YTHomeArticle *article = self.model.list[indexPath.row];
    
    YTHomeDetailController *detailVC = [[YTHomeDetailController alloc] init];
    detailVC.ID = article.ID;
    [self.navigationController pushViewController:detailVC animated:true];
}

@end
